//
//  MBProgressHUD+Ex.m
//  QianLongZan
//
//  Created by 陈家将 on 16/9/28.
//  Copyright © 2016年 szteyou. All rights reserved.
//

#define SuccessImage @"success.png"
#define ErrorImage @"error.png"

#import "MBProgressHUD+Ex.h"

@implementation MBProgressHUD (Ex)

// 纯菊花模式
+ (void)show {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:KEY_WINDOW animated:YES];
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.bezelView.backgroundColor = COLOR_CLEAR;
    hud.animationType = MBProgressHUDAnimationZoomOut;
    //    [UIActivityIndicatorView appearanceWhenContainedIn:[MBProgressHUD class], nil].color = [UIColor greenColor];
}

+ (void)hide {
    [MBProgressHUD hideHUDForView:KEY_WINDOW animated:YES];
}

+ (void)showInView:(UIView *)view {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.bezelView.backgroundColor = COLOR_CLEAR;
    hud.animationType = MBProgressHUDAnimationZoomOut;
}

+ (void)hideFromView:(UIView *)view {
    dispatch_sync_main_safe(^{
        [MBProgressHUD hideHUDForView:view animated:YES];
    })
}

// 纯文本模式
+ (void)showMsg:(NSString *)msg {
    [MBProgressHUD hide];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:KEY_WINDOW animated:YES];
    hud.animationType = MBProgressHUDAnimationZoomOut;
    hud.mode = MBProgressHUDModeText;
    hud.label.text = msg;
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [MBProgressHUD hideFromView:KEY_WINDOW];
    });
}

+ (void)showMsg:(NSString *)msg detail:(NSString *)detail after:(CGFloat)times {
    [MBProgressHUD hide];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:KEY_WINDOW animated:YES];
    hud.animationType = MBProgressHUDAnimationZoomOut;
    hud.mode = MBProgressHUDModeText;
    hud.label.text = msg;
    hud.detailsLabel.text = detail;
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(times * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [MBProgressHUD hideFromView:KEY_WINDOW];
    });
}

+ (void)showMsg:(NSString *)msg detail:(NSString *)detail {
    [MBProgressHUD hide];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:KEY_WINDOW animated:YES];
    hud.animationType = MBProgressHUDAnimationZoomOut;
    hud.mode = MBProgressHUDModeText;
    hud.label.text = msg;
    hud.detailsLabel.text = detail;
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [MBProgressHUD hideFromView:KEY_WINDOW];
    });
}

+ (void)showMsg:(NSString *)msg detail:(NSString *)detail inView:(UIView *)view {
    [MBProgressHUD hideFromView:view];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.animationType = MBProgressHUDAnimationZoomOut;
    hud.mode = MBProgressHUDModeText;
    hud.label.text = msg;
    hud.detailsLabel.text = detail;
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [MBProgressHUD hideFromView:view];
    });
}


+ (void)showMsg:(NSString *)msg inView:(UIView *)view {
    [MBProgressHUD hideFromView:view];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.animationType = MBProgressHUDAnimationZoomOut;
    hud.mode = MBProgressHUDModeText;
    hud.label.text = msg;
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [MBProgressHUD hideFromView:view];
    });
}

// 菊花文本模式
+ (void)showLoadingWithMsg:(NSString *)msg {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:KEY_WINDOW animated:YES];
    hud.animationType = MBProgressHUDAnimationZoomOut;
    hud.label.text = msg;
}

+ (void)showLoadingWithMsg:(NSString *)msg detail:(NSString *)detail {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:KEY_WINDOW animated:YES];
    hud.animationType = MBProgressHUDAnimationZoomOut;
    hud.label.text = msg;
    hud.detailsLabel.text = detail;
}

+ (void)showLoadingWithMsg:(NSString *)msg inView:(UIView *)view {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.animationType = MBProgressHUDAnimationZoomOut;
    hud.label.text = msg;
}

+ (void)showLoadingWithMsg:(NSString *)msg detail:(NSString *)detail inView:(UIView *)view {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.animationType = MBProgressHUDAnimationZoomOut;
    hud.label.text = msg;
    hud.detailsLabel.text = detail;
}

// 带图片的模式
+ (void)show:(NSString *)text icon:(NSString *)icon view:(UIView *)view {
    if (view == nil) view = [UIApplication sharedApplication].keyWindow;
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.label.text = text;
    // 设置图片
    hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:icon]];
    // 再设置模式
    hud.mode = MBProgressHUDModeCustomView;
    
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    
    // 1.5f秒之后再消失
    [hud hideAnimated:YES afterDelay:1.5f];
}

+ (void)show:(NSString *)text icon:(NSString *)icon toYOffset:(float)yOffset view:(UIView *)view {
    if (view == nil) view = [UIApplication sharedApplication].keyWindow;
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.label.text = text;
    // 设置图片
    hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:icon]];
    // 再设置模式
    hud.mode = MBProgressHUDModeCustomView;
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    // 1秒之后再消失
    [hud hideAnimated:YES afterDelay:1.5f];
}


+ (void)showSuccessInView:(UIView *)view {
    if (view == nil) view = [UIApplication sharedApplication].keyWindow;
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    // 设置图片
    hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:SuccessImage]];
    // 再设置模式
    hud.mode = MBProgressHUDModeCustomView;
    
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    
    
    // 1.5f秒之后再消失
    [hud hideAnimated:YES afterDelay:1.5f];
}

+ (void)showErrorInView:(UIView *)view {
    if (view == nil) view = [UIApplication sharedApplication].keyWindow;
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    // 设置图片
    hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:ErrorImage]];
    // 再设置模式
    hud.mode = MBProgressHUDModeCustomView;
    
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    
    // 1.5f秒之后再消失
    [hud hideAnimated:YES afterDelay:1.5f];
}

+ (void)showError:(NSString *)error toView:(UIView *)view {
    [self show:error icon:ErrorImage view:view];
}

+ (void)showSuccess:(NSString *)success toView:(UIView *)view {
    [self show:success icon:SuccessImage view:view];
}

+ (void)showError:(NSString *)error toYOffset:(float)yOffset toView:(UIView *)view {
    [self show:error icon:ErrorImage toYOffset:yOffset view:view];
}

+ (void)showSuccess:(NSString *)success toYOffset:(float)yOffset toView:(UIView *)view {
    [self show:success icon:SuccessImage toYOffset:yOffset view:view];
}

@end
